package com.softa.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.softa.dao.model.SysServer;
import com.softa.service.system.SysServerService;
import com.softa.service.system.UserService;
import com.softa.utils.Constants;
import com.softa.vo.DataGridRequest;
import com.softa.vo.DataGridResponse;
import com.softa.vo.QueryRequest;
import com.softa.web.controller.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/sysserver")
public class SysServerController extends BaseController {

    @Resource
    private SysServerService sysServerService;

    @RequiresPermissions("sysserver:view")
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model) {
        return Constants.MENU_MANAGE+"/sysserver/list";
    }

    @RequiresPermissions("sysserver:view")
    @RequestMapping(value="/listPage",method = RequestMethod.POST)
    @ResponseBody
    public DataGridResponse listPage(DataGridRequest request, QueryRequest queryRequest) {
        PageInfo<SysServer> pageResult = sysServerService.queryUserByParam(request, queryRequest);
        return new DataGridResponse(pageResult);
    }

    @RequiresPermissions("sysserver:view")
    @RequestMapping(value="/addsysserverpage",method = RequestMethod.GET)
    public String addsysserverpage(String param, Model model) {
        return Constants.MENU_MANAGE+"/sysserver/add_sysserver";
    }

    @RequiresPermissions("sysserver:view")
    @RequestMapping(value="/editsysserverpage",method = RequestMethod.GET)
    public String edituserpage() {
        return Constants.MENU_MANAGE+"/sysserver/add_sysserver";
    }

    @RequiresPermissions("sysserver:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public String create(SysServer sysServer) {
        String message="录入成功";
        try {
            message=sysServerService.createSysServer(sysServer);
        } catch (Exception e) {
            message="录入失败";
            logger.error(e.getMessage());
        }
        return message;
    }

    @RequiresPermissions("sysserver:update")
    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    @ResponseBody
    public String update(SysServer sysServer) {
        String message="修改成功";
        try {
            message=sysServerService.updateSysServer(sysServer);
        } catch (Exception e) {
            message="修改失败";
            logger.error(e.getMessage());
        }
        return message;
    }

    @RequiresPermissions("sysserver:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete(@PathVariable("id") Long id) {
        String message="删除成功";
        try {
            sysServerService.deleteSysServer(id);
        } catch (Exception e) {
            message="删除失败";
            logger.error(e.getMessage());
        }
        return message;
    }
}
